module.exports = function (s, o) {
  if (s == null || s == undefined) s = '';
  if(s && typeof s == 'object') s = JSON.stringify(s)
  s = new String(s);
  s.replaceAll = function (k, v) {
    return this.split(k).join(v);
  }
  s.fixInt = function(num){
    num = num || 2
    let pow = Math.pow(10,num)
    return parseInt(this * pow) / pow
  }
  s.max = function (n, r) {
    r = r || "...";
    var _this = this;
    _this = _this.length > n ? _this.substr(0, n - r.length) + r : _this;
    return _this;
  }
  s._eval = function (o, fn, finalFunction) {
    var _this = this;
    _this = _this.replace(new RegExp('{{', 'gmi'), '{').replace(new RegExp('}}', 'gmi'), '}');
    for (var k in o) {
      var v = o[k];
      v = v == undefined || v == null ? '' : v;
      _this = _this.replace(new RegExp("{()" + k + "}", "g"), v); //直接替换
      _this = _this.replace(new RegExp("{listStyle_" + k + "}", "g"), typeof v === "string" ? v.charAt(0) : "?"); //列表风格
      if (typeof fn == 'function') {
        _this = args(_this, k, v, o);
      }
    }
    if (typeof finalFunction == 'function') _this = finalFunction(_this, o);
    return _this;
  }
  s.dhms = function (type) {
    var _s = -(-s),
      rc = '';
    var oDay = parseInt(_s / (24 * 60 * 60));
    rc = oDay > 0 ? oDay + '天' : '';
    var oHours = parseInt(_s / (60 * 60) % 24);
    if (rc || oHours > 0) rc += oHours + '时';
    var oMinutes = parseInt(_s / 60 % 60);
    if (rc || oMinutes > 0) rc += oMinutes + '分';
    var oSeconds = parseInt(_s % 60);
    if (rc || oSeconds > 0) rc += oSeconds + '秒';
    if (type == 'Array') {
      rc = [oDay, oHours, oMinutes, oSeconds];
    } else if (type == 'Object') {
      rc = {
        d: oDay,
        h: oHours,
        m: oMinutes,
        s: oSeconds
      }
    }
    return rc;
  }
  s.Format = function () {
    var _this = this;
    for (var i = 0; i < arguments.length; i++) _this = _this.replace(/%(s|d)/, arguments[i] == undefined ? '' : arguments[i]);
    return _this;
  }
  s.parse = function (error, replace) {
    try {
      if (s) {
        if (typeof replace == 'function') s = replace(s);
        return JSON.parse(s);
      } else {
        return error || {};
      }
    } catch (e) {
      return error || {};
    }
  }
  s.urlParse = function (flog) {
    var query = {};
    var _query = decodeURIComponent(s).split(flog || '&');
    for (var i = 0; i < _query.length; i++) {
      _query[i] = _query[i].split('=');
      if(/^[0-9]+$/.test(_query[i][1])) _query[i][1] = -(-_query[i][1]);
      query[_query[i][0]] = _query[i][1] == undefined ? '' : _query[i][1];
    }
    return query;
  }
  if (o instanceof Array) {
    return s.Format.apply(s, o);
  } else if (o && typeof o == 'object') {
    return s._eval.call(s, o);
  } else {
    return s;
  }
}